package com.bzkj.basicinfo.service.impl;

import com.bzkj.basicinfo.domain.basic.WmsBasicBillsTypeEntity;
import com.bzkj.basicinfo.domain.basic.vo.NccBasicBillsTypeTree;
import com.bzkj.basicinfo.mapper.NccBasicBillsTypeMapper;
import com.bzkj.basicinfo.mapper.WmsBasicBillsTypeMapper;
import com.bzkj.basicinfo.service.WmsBasicBillsTypeService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wl
 * @description 针对表【wms_basic_bills_type(单据二次分类)】的数据库操作Service实现
 * @createDate 2025-01-14 17:05:22
 */
@Slf4j
@Service
public class WmsBasicBillsTypeServiceImpl implements WmsBasicBillsTypeService
{

    @Autowired
    private WmsBasicBillsTypeMapper wmsBasicBillsTypeMapper;

    @Autowired
    private NccBasicBillsTypeMapper nccBasicBillsTypeMapper;

    /**
     * 查询单据二次分类数据
     *
     * @return
     */
    @Override
    public R findAllBasicBillsType()
    {
        //获取wms系统使用的单据类型数据
        List<NccBasicBillsTypeTree> list = nccBasicBillsTypeMapper.findAllBasicBillsType();

        List<WmsBasicBillsTypeEntity> wbbt = wmsBasicBillsTypeMapper.findAllBasicBillsType();

        Map<Integer, NccBasicBillsTypeTree> collect = list.stream().collect(Collectors.toMap(NccBasicBillsTypeTree :: getId, Function.identity()));

        wbbt.forEach(item -> {
            NccBasicBillsTypeTree temp = collect.get(item.getParentId());
            if(! Objects.isNull(temp)) temp.getChildren().add(item);
        });

        return R.ok(list);
    }

    /**
     * 新增单据二次分类信息
     *
     * @param wmsBasicBillsTypeEntity
     * @return
     */
    @Override
    public AjaxResult saveBasicBillsType(WmsBasicBillsTypeEntity wmsBasicBillsTypeEntity)
    {
        try {
            wmsBasicBillsTypeMapper.saveBasicBillsType(wmsBasicBillsTypeEntity);
            return AjaxResult.success("操作成功");
        } catch(Exception e) {
            log.error("新增单据二次分类信息失败:{}",e);
            return AjaxResult.error("操作失败");
        }
    }

    /**
     * 修改单据二次分类信息
     * @param wmsBasicBillsTypeEntity
     * @return
     */
    @Override
    public R putBasicBillsType(WmsBasicBillsTypeEntity wmsBasicBillsTypeEntity)
    {
        try {
            wmsBasicBillsTypeMapper.putBasicBillsType(wmsBasicBillsTypeEntity);
            return R.ok("操作成功");
        } catch(Exception e) {
            log.error("修改单据二次分类信息失败:{}",e);
            return R.fail("操作失败");
        }
    }

    /**
     * 修改单据二次分类状态
     * @param id
     * @param enableStatus
     * @return
     */
    @Override
    public R putBasicBillsEnableStatus(Integer id, Integer enableStatus)
    {
        try {
            wmsBasicBillsTypeMapper.putBasicBillsEnableStatus(id,enableStatus);
            return R.ok("操作成功");
        } catch(Exception e) {
            log.error("修改单据二次分类状态失败:{}",e);
            return R.fail("操作失败");
        }
    }
}




